//注册
package com.example.wuyeguanli.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.wuyeguanli.R;
import com.example.wuyeguanli.entiy.Owner;
import com.example.wuyeguanli.entiy.Register;
import com.example.wuyeguanli.entiy.Response;
import com.example.wuyeguanli.entiy.Room;
import com.example.wuyeguanli.tools.ApiClient;
import com.example.wuyeguanli.tools.DataValidator;

public class RegisterActivity extends AppCompatActivity {
    private final String TAG = "ApiClient";
    private EditText user;
    private EditText room_num;
    private EditText phone_num;
    private EditText password;
    private EditText password_judge;
    private Button btn_register;
    private Button btn_back;
    private ProgressBar loading_progress;
    private final ApiClient apiClient = new ApiClient();

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accountregistration);

        //隐藏标题栏
        if(getSupportActionBar() != null){
            getSupportActionBar().hide();
        }

        user = findViewById(R.id.user);
        room_num = findViewById(R.id.room_num);
        phone_num = findViewById(R.id.phone_num);
        password = findViewById(R.id.password);
        password_judge = findViewById(R.id.password_judge);
        btn_register = findViewById(R.id.btn_register);
        btn_back = findViewById(R.id.btn_back);
        loading_progress = findViewById(R.id.loading_progress);

        btn_back.setOnClickListener(v -> finish());

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setLoadingState(true);

                if (!password.getText().toString().equals(password_judge.getText().toString())) {
                    setLoadingState(false);
                    Toast.makeText(RegisterActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!DataValidator.isValidName(user.getText().toString())) {
                    setLoadingState(false);
                    Toast.makeText(RegisterActivity.this, "用户名要求2-10位中文", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!DataValidator.isValidPassword(password.getText().toString())) {
                    setLoadingState(false);
                    Toast.makeText(RegisterActivity.this, "密码要求6-12位字母或数字", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!DataValidator.isValidRoomNumber(room_num.getText().toString())) {
                    setLoadingState(false);
                    Toast.makeText(RegisterActivity.this, "房间号要求6位数字", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!DataValidator.isValidPhoneNumber(phone_num.getText().toString())) {
                    setLoadingState(false);
                    Toast.makeText(RegisterActivity.this, "电话号要求11位数字且第一位数字不为0", Toast.LENGTH_SHORT).show();
                    return;
                }

                Register register = new Register(
                        phone_num.getText().toString(),
                        password.getText().toString(),
                        user.getText().toString(),
                        room_num.getText().toString()
                );
                apiClient.register(register, new ApiClient.OnCompleteListener<Response<Object>>() {
                    @Override
                    public void onSuccess(Response<Object> data) {
                        setLoadingState(false);
                        if (data.getState().equals("success")) {
                            finish();
                        } else {
                            Toast.makeText(RegisterActivity.this, data.getError(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void OnError(Throwable error) {
                        setLoadingState(false);
                        Log.e(TAG, "OnError: ", error);
                        Toast.makeText(RegisterActivity.this, "网络错误，请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    private void setLoadingState(boolean isLoading) {
        if (isLoading) {
            loading_progress.setVisibility(View.VISIBLE);
            user.setEnabled(false);
            room_num.setEnabled(false);
            password.setEnabled(false);
            password_judge.setEnabled(false);
            btn_register.setEnabled(false);
        } else {
            loading_progress.setVisibility(View.GONE);
            user.setEnabled(true);
            room_num.setEnabled(true);
            password.setEnabled(true);
            password_judge.setEnabled(true);
            btn_register.setEnabled(true);
        }
    }
}
